home *** CD-ROM | disk | FTP | other *** search
- // GlueWindowCDEV4.1.c
-
- // inclusion of files -----------------------------
-
- #include <OSUtils.h>
- #include "GlueWindow4.1.h"
-
- // definition -------------------------------------
-
- enum { k_about = 1,
- k_drag_on,
- k_grow_on,
- k_push_on,
- k_push_sound,
- k_push_ctrl,
- k_push_shift,
- k_push_option,
- k_push_command,
- b_push_ctrl,
- b_push_shift,
- b_push_option,
- b_push_command,
- k_pop_on,
- k_pop_sound,
- k_pop_ctrl,
- k_pop_shift,
- k_pop_option,
- k_pop_command,
- b_pop_ctrl,
- b_pop_shift,
- b_pop_option,
- b_pop_command,
- k_show_icon,
- k_no_marquee,
- k_drag_ctrl,
- k_drag_shift,
- k_drag_option,
- k_drag_command,
- b_drag_ctrl,
- b_drag_shift,
- b_drag_option,
- b_drag_command };
-
- #define ALERT_RSRC 0
- #define ABOUT_RSRC 1
- #define RESTART_RSRC 2
- #define SAME_RSRC 3
- #define COMMAND_RSRC 4
- #define NO_7_RSRC 5
- #define WINDOW_ICON 2
- #define key1 (**ch).temp_init_data.push_key
- #define key2 (**ch).temp_init_data.pop_key
- #define key3 (**ch).temp_init_data.drag_key
-
- // declaration of prototypes ----------------------
-
- pascal long main(short msg, short item, short numItems,
- short panelId, EventRecord *evntp, long val,
- DialogPtr dp);
- Boolean is_system7(void);
- Handle Get1Resource_in_system_heap(OSType rType, short id);
-
- short NumToolboxTraps( void );
- TrapType GetTrapType(short theTrap);
- Boolean TrapAvailable(short theTrap);
-
- long do_check(DialogPtr dp);
- long do_init (DialogPtr dp, short numItems);
- init_data_hdl load_setting(void);
- int open_prefs_file(prefs_file *pf);
- void save_setting(init_data_hdl save_data_hdl, prefs_file *pf_ptr);
- void do_close(cdev_data_hdl ch);
-
- void do_hit (cdev_data_hdl ch, DialogPtr dp, short hit_item,
- EventRecord *evntp, short numItems);
- void apply_setting (cdev_data_hdl cur_data_hdl);
- void setup_ditems (cdev_data_hdl ch, DialogPtr dp, short numItems);
- void hilite_btn (cdev_data_hdl ch, DialogPtr dp, short numItems);
- void set_check_btn (DialogPtr dp, short item_num, Boolean check);
- void check_key_combination(cdev_data_hdl ch, short key_num);
- void about(short id, void *p);
-
- // main routines ----------------------------------
-
- pascal long main(short msg, short item, short numItems, short panelId,
- EventRecord *evntp, long val, DialogPtr dp)
- {
- long rv;
-
- rv = val;
- switch(msg) {
- case nulDev:
- case activDev:
- case deactivDev:
- case keyEvtDev:
- case undoDev:
- case cutDev:
- case copyDev:
- case pasteDev:
- case clearDev:
- case updateDev:
- break;
- case macDev:
- rv = do_check(dp);
- break;
- case initDev:
- rv = do_init(dp, numItems);
- break;
- case closeDev:
- do_close((cdev_data_hdl)val);
- break;
- case hitDev:
- do_hit((cdev_data_hdl)val, dp, item + numItems, evntp, numItems);
- break;
- }
- return(rv);
- }
-
- Boolean is_system7(void)
- {
- Boolean f;
- OSErr err;
- long r;
-
- if(f = TrapAvailable(_Gestalt)) {
- err = Gestalt(gestaltSystemVersion, &r);
- if(err != noErr) f = false;
- else if(r <0x0700) f = false;
- else f = true;
- }
- return(f);
- }
-
- Handle Get1Resource_in_system_heap(OSType rType, short id)
- {
- THz oldZone;
- Handle h;
-
- oldZone = GetZone();
- SetZone(SystemZone());
- h = Get1Resource(rType, id);
- SetZone(oldZone);
- return(h);
- }
-
- // gestalt check routines -------------------------
-
- #define TrapMask 0x0800
-
- short NumToolboxTraps( void )
- {
- if (NGetTrapAddress(_InitGraf, ToolTrap) ==
- NGetTrapAddress(0xAA6E, ToolTrap))
- return(0x0200);
- else
- return(0x0400);
- }
-
- TrapType GetTrapType(short theTrap)
- {
-
- if ((theTrap & TrapMask) > 0)
- return(ToolTrap);
- else
- return(OSTrap);
-
- }
-
- Boolean TrapAvailable(short theTrap)
- {
-
- TrapType tType;
-
- tType = GetTrapType(theTrap);
- if (tType == ToolTrap)
- theTrap = theTrap & 0x07FF;
- if (theTrap >= NumToolboxTraps())
- theTrap = _Unimplemented;
-
- return (NGetTrapAddress(theTrap, tType) !=
- NGetTrapAddress(_Unimplemented, ToolTrap));
- }
-
- // event routines ---------------------------------
-
- // open and close routines ------------------------
-
- long do_check(DialogPtr dp)
- {
- Handle h;
- long is_ok = 0;
-
- SetPort(dp);
- if( TrapAvailable(_Gestalt) ) {
- if( is_system7() ) {
- is_ok = 1;
- // When you receive macDev message, you should check
- // your control panel can run. If it can, return 1.
- // If cannot, return 0.
- }
- }
- else
- Alert(RSRC_NUMBER_BASE + NO_7_RSRC, nil);
-
- return(is_ok);
- }
-
- long do_init(DialogPtr dp, short numItems)
- {
- cdev_data_hdl ch;
- init_data_hdl ih;
- Handle h;
-
- SetPort(dp);
- ih = load_setting();
- if(!ih) Alert(RSRC_NUMBER_BASE + ALERT_RSRC, nil);
- ch = (cdev_data_hdl)NewHandle(sizeof(cdev_data));
- (**ch).temp_init_data = **ih;
- DisposeHandle((Handle)ih);
- setup_ditems(ch , dp, numItems);
- return((long)ch);
- }
-
- init_data_hdl load_setting(void)
- {
- init_data_hdl h = nil, sysheap_data_hdl = nil;
- old_init_data_hdl old_h = nil;
- short save_ref_num;
- prefs_file pf;
- int er;
- OSErr err;
- long address;
-
- err = Gestalt(CREATOR, &address);
-
- if(err == NO_ERROR) {
- h = (init_data_hdl)NewHandle(sizeof(init_data));
- sysheap_data_hdl = (init_data_hdl)address;
- **h = **sysheap_data_hdl;
- }
- else {
- save_ref_num = CurResFile();
- er = open_prefs_file(&pf);
- if(er != CAUTION_ERROR) {
- if(er == NO_ERROR) {
- h = (init_data_hdl)Get1Resource_in_system_heap(SETTING_RSRC, RSRC_NUMBER_BASE + 1);
- if(h) {
- HNoPurge((Handle)h);
- DetachResource((Handle)h);
- }
- else {
- old_h = (old_init_data_hdl)Get1Resource(SETTING_RSRC, RSRC_NUMBER_BASE);
- if(old_h) {
- h = (init_data_hdl)NewHandleSys(sizeof(init_data));
- if(h) {
- (**h).drag_on = (**(init_data_hdl)old_h).drag_on;
- (**h).grow_on = (**(init_data_hdl)old_h).grow_on;
- (**h).push_on = (**(init_data_hdl)old_h).push_on;
- (**h).pop_on = (**(init_data_hdl)old_h).pop_on;
- (**h).push_sound = (**(init_data_hdl)old_h).push_sound;
- (**h).pop_sound = (**(init_data_hdl)old_h).pop_sound;
- (**h).push_key[0] = (**(init_data_hdl)old_h).push_key[0];
- (**h).push_key[1] = (**(init_data_hdl)old_h).push_key[1];
- (**h).push_key[2] = (**(init_data_hdl)old_h).push_key[2];
- (**h).push_key[3] = (**(init_data_hdl)old_h).push_key[3];
- (**h).pop_key[0] = (**(init_data_hdl)old_h).pop_key[0];
- (**h).pop_key[1] = (**(init_data_hdl)old_h).pop_key[1];
- (**h).pop_key[2] = (**(init_data_hdl)old_h).pop_key[2];
- (**h).pop_key[3] = (**(init_data_hdl)old_h).pop_key[3];
- (**h).show_init_icon= (**(init_data_hdl)old_h).show_init_icon;
- (**h).no_marquee = false;
- (**h).drag_key[0] = false;
- (**h).drag_key[1] = false;
- (**h).drag_key[2] = false;
- (**h).drag_key[3] = true;
- ReleaseResource((Handle)old_h);
- save_setting(h, &pf);
- }
- }
- }
- CloseResFile(pf.RsrcRefNum);
- }
- else if(er == OPEN_ERROR) {
- h = (init_data_hdl)NewHandleSys(sizeof(init_data));
- if(h) {
- (**h).drag_on = true;
- (**h).grow_on = true;
- (**h).push_on = true;
- (**h).pop_on = true;
- (**h).push_sound = true;
- (**h).pop_sound = true;
- (**h).push_key[0] = false;
- (**h).push_key[1] = true;
- (**h).push_key[2] = false;
- (**h).push_key[3] = false;
- (**h).pop_key[0] = true;
- (**h).pop_key[1] = false;
- (**h).pop_key[2] = false;
- (**h).pop_key[3] = false;
- (**h).show_init_icon= true;
- (**h).no_marquee = false;
- (**h).drag_key[0] = false;
- (**h).drag_key[1] = false;
- (**h).drag_key[2] = false;
- (**h).drag_key[3] = true;
- save_setting(h, &pf);
- }
- }
- UseResFile(save_ref_num);
- Alert(RSRC_NUMBER_BASE + RESTART_RSRC, nil);
- }
- UseResFile(save_ref_num);
- }
- return(h);
- }
-
- int open_prefs_file(prefs_file *pf)
- {
- OSErr er;
- int rn;
-
- er = FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder,
- &(pf->vRefNum), &(pf->DirID));
- if(er == 0) {
- rn = HOpenResFile(pf->vRefNum, pf->DirID, PREFS_TITLE, fsRdWrPerm);
- if(rn != -1) {
- pf->RsrcRefNum = rn;
- return(NO_ERROR);
- }
- return(OPEN_ERROR);
- }
- return(CAUTION_ERROR);
- }
-
- void save_setting(init_data_hdl sysheap_data_hdl, prefs_file *pf_ptr)
- {
- Handle h;
- init_data_hdl work_data_hdl;
- FInfo f_info;
-
- HCreateResFile(pf_ptr->vRefNum, pf_ptr->DirID, PREFS_TITLE);
- f_info.fdType = 'pref';
- f_info.fdCreator = CREATOR;
- f_info.fdFlags = 0;
- HSetFInfo(pf_ptr->vRefNum, pf_ptr->DirID, PREFS_TITLE, &f_info);
- // You must call 'open_prefs_file' again.
- // Because HSetFInfo closes a file.
- open_prefs_file(pf_ptr);
-
- work_data_hdl = (init_data_hdl)NewHandle(sizeof(init_data));
- if(work_data_hdl) {
- **work_data_hdl = **sysheap_data_hdl;
- if(h = Get1Resource(SETTING_RSRC, RSRC_NUMBER_BASE + 1))
- RmveResource(h);
- AddResource((Handle)work_data_hdl, SETTING_RSRC, RSRC_NUMBER_BASE + 1, "\p");
- ReleaseResource((Handle)work_data_hdl);
- CloseResFile(CurResFile());
- }
- }
-
- void do_close(cdev_data_hdl ch)
- {
- prefs_file pf;
-
- open_prefs_file(&pf);
- if( ((**ch).temp_init_data.push_on == true) &&
- ((**ch).temp_init_data.pop_on == true) &&
- (key1[0] == key2[0]) &&
- (key1[1] == key2[1]) &&
- (key1[2] == key2[2]) &&
- (key1[3] == key2[3]) ) {
- Alert(RSRC_NUMBER_BASE + SAME_RSRC, nil);
- (**ch).temp_init_data.pop_on = false;
- }
- apply_setting(ch);
- save_setting((init_data_hdl)ch, &pf);
- DisposeHandle((Handle)ch);
- }
-
- // other event routines ---------------------------
-
- void do_hit(cdev_data_hdl ch, DialogPtr dp, short hit_item,
- EventRecord *evntp, short numItems)
- {
- switch(hit_item) {
- case k_drag_on:
- (**ch).temp_init_data.drag_on = !(**ch).temp_init_data.drag_on;
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case k_grow_on:
- (**ch).temp_init_data.grow_on = !(**ch).temp_init_data.grow_on;
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case k_push_on:
- (**ch).temp_init_data.push_on = !(**ch).temp_init_data.push_on;
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case k_pop_on:
- (**ch).temp_init_data.pop_on = !(**ch).temp_init_data.pop_on;
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case k_push_sound:
- (**ch).temp_init_data.push_sound = !(**ch).temp_init_data.push_sound;
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case k_pop_sound:
- (**ch).temp_init_data.pop_sound = !(**ch).temp_init_data.pop_sound;
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case k_show_icon:
- (**ch).temp_init_data.show_init_icon = !(**ch).temp_init_data.show_init_icon;
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case k_push_ctrl:
- case k_push_shift:
- case k_push_option:
- case k_push_command:
- key1[hit_item - k_push_ctrl] = !key1[hit_item - k_push_ctrl];
- check_key_combination(ch, hit_item - k_push_ctrl);
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case k_pop_ctrl:
- case k_pop_shift:
- case k_pop_option:
- case k_pop_command:
- key2[hit_item - k_pop_ctrl] = !key2[hit_item - k_pop_ctrl];
- check_key_combination(ch, hit_item - k_pop_ctrl);
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case b_push_ctrl:
- case b_push_shift:
- case b_push_option:
- case b_push_command:
- key1[hit_item - b_push_ctrl] = !key1[hit_item - b_push_ctrl];
- check_key_combination(ch, hit_item - b_push_ctrl);
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case b_pop_ctrl:
- case b_pop_shift:
- case b_pop_option:
- case b_pop_command:
- key2[hit_item - b_pop_ctrl] = !key2[hit_item - b_pop_ctrl];
- check_key_combination(ch, hit_item - b_pop_ctrl);
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case k_no_marquee:
- (**ch).temp_init_data.no_marquee = !(**ch).temp_init_data.no_marquee;
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case k_drag_ctrl:
- case k_drag_shift:
- case k_drag_option:
- case k_drag_command:
- key3[hit_item - k_drag_ctrl] = !key3[hit_item - k_drag_ctrl];
- check_key_combination(ch, hit_item - k_drag_ctrl);
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case b_drag_ctrl:
- case b_drag_shift:
- case b_drag_option:
- case b_drag_command:
- key3[hit_item - b_drag_ctrl] = !key3[hit_item - b_drag_ctrl];
- check_key_combination(ch, hit_item - b_drag_ctrl);
- setup_ditems(ch, dp, numItems);
- apply_setting(ch);
- break;
- case k_about:
- about(RSRC_NUMBER_BASE + ABOUT_RSRC, nil);
- break;
- }
- }
-
- void apply_setting(cdev_data_hdl cur_data_hdl)
- {
- init_data_hdl sysheap_data_hdl;
- OSErr err;
- long address;
-
-
- err = Gestalt(CREATOR, &address);
- if(err == NO_ERROR) {
- sysheap_data_hdl = (init_data_hdl)address;
- **sysheap_data_hdl = (**cur_data_hdl).temp_init_data;
- }
- }
-
- void setup_ditems(cdev_data_hdl ch, DialogPtr dp, short numItems)
- {
- hilite_btn(ch, dp, numItems);
-
- set_check_btn(dp, k_drag_on + numItems, (**ch).temp_init_data.drag_on);
- set_check_btn(dp, k_grow_on + numItems, (**ch).temp_init_data.grow_on);
- set_check_btn(dp, k_push_on + numItems, (**ch).temp_init_data.push_on);
- set_check_btn(dp, k_pop_on + numItems, (**ch).temp_init_data.pop_on);
- set_check_btn(dp, k_push_sound + numItems, (**ch).temp_init_data.push_sound);
- set_check_btn(dp, k_pop_sound + numItems, (**ch).temp_init_data.pop_sound);
- set_check_btn(dp, k_push_ctrl + numItems, key1[0]);
- set_check_btn(dp, k_push_shift + numItems, key1[1]);
- set_check_btn(dp, k_push_option + numItems, key1[2]);
- set_check_btn(dp, k_push_command+ numItems, key1[3]);
- set_check_btn(dp, k_pop_ctrl + numItems, key2[0]);
- set_check_btn(dp, k_pop_shift + numItems, key2[1]);
- set_check_btn(dp, k_pop_option + numItems, key2[2]);
- set_check_btn(dp, k_pop_command + numItems, key2[3]);
- set_check_btn(dp, k_show_icon + numItems, (**ch).temp_init_data.show_init_icon);
- set_check_btn(dp, k_no_marquee + numItems, (**ch).temp_init_data.no_marquee);
- set_check_btn(dp, k_drag_ctrl + numItems, key3[0]);
- set_check_btn(dp, k_drag_shift + numItems, key3[1]);
- set_check_btn(dp, k_drag_option + numItems, key3[2]);
- set_check_btn(dp, k_drag_command+ numItems, key3[3]);
- // These are not clever :-)
- }
-
- void hilite_btn(cdev_data_hdl ch, DialogPtr dp, short numItems)
- {
- short item_type;
- Handle h;
- Rect r;
- int i, button;
-
- button = k_push_sound;
- for( i = 0; i <= 4; i++) {
- GetDItem(dp, numItems + button + i, &item_type, &h, &r);
- HiliteControl((ControlHandle)h, (!(**ch).temp_init_data.push_on) * 255);
- }
- button = k_pop_sound;
- for( i = 0; i <= 4; i++) {
- GetDItem(dp, numItems + button + i, &item_type, &h, &r);
- HiliteControl((ControlHandle)h, (!(**ch).temp_init_data.pop_on) * 255);
- }
- button = k_drag_ctrl;
- for( i = 0; i <= 3; i++) {
- GetDItem(dp, numItems + button + i, &item_type, &h, &r);
- HiliteControl((ControlHandle)h,
- (!( (**ch).temp_init_data.no_marquee * (**ch).temp_init_data.drag_on) ) * 255);
- }
- GetDItem(dp, numItems + k_no_marquee, &item_type, &h, &r);
- HiliteControl((ControlHandle)h, (!(**ch).temp_init_data.drag_on) * 255);
- }
-
- void set_check_btn(DialogPtr dp, short item_num, Boolean is_on)
- {
- short item_type;
- Handle h;
- Rect r;
-
- GetDItem(dp, item_num, &item_type, &h, &r);
- if(is_on) SetCtlValue((ControlHandle)h, 1);
- else SetCtlValue((ControlHandle)h, 0);
- }
-
- void check_key_combination(cdev_data_hdl ch, short key_num)
- {
- if( (key1[0] == false) &&
- (key1[1] == false) &&
- (key1[2] == false) &&
- (key1[3] == false) ) {
- key1[key_num] = true;
- }
- if( (key2[0] == false) &&
- (key2[1] == false) &&
- (key2[2] == false) &&
- (key2[3] == false) ) {
- key2[key_num] = true;
- }
- }
-
-
- void about(short id, void *p)
- {
- GrafPtr saved_port;
- PenState saved_pen_state;
- WindowPtr wp;
- EventRecord an_event;
- KeyMap my_keymap;
- PicHandle title_hdl;
- Handle icon_hdl;
- Rect r;
- Str255 str;
- int i;
- long ticks, num_of_str;
- short fh, fv;
- Pattern gray_pat;
- Boolean exit_draw = false, color_icon, color_qd;
- RGBColor fore_color, back_color;
-
- gray_pat[0]= 0x55;
- for(i = 1; i <8; i++) gray_pat[i] = ~gray_pat[i-1];
-
- // I use GetNewCWindow, not GetDialog. Because StaticText of a dialog
- // displays 12pt of geneva, but I want to display text with 9pt.
- // If you don't want to use other than 12pt, use GetDialog. Fonts can be
- // changed with SetDAFont ( New Inside Macintosh: SetDialogFont ).
- GetPort(&saved_port);
- GetPenState(&saved_pen_state);
- if(color_qd = TrapAvailable(_GetForeColor)) {
- GetForeColor(&fore_color);
- GetBackColor(&back_color);
- }
- SetRect(&r, 5, 5, 148, 35);
- wp = GetNewCWindow(id, 0, (WindowPtr)-1);
- ShowWindow(wp);
- SetPort(wp);
- title_hdl = GetPicture(RSRC_NUMBER_BASE);
- DrawPicture(title_hdl, &r);
- PenNormal();
- TextSize(9);
- GetIndString(str, RSRC_NUMBER_BASE + 1, 1);
- StringToNum(str, &num_of_str);
- for(i = 0; i <= num_of_str; i++) {
- GetIndString(str, RSRC_NUMBER_BASE, i + 1);
- MoveTo(20, 45 + i * 12);
- DrawString(str);
- }
- SetRect(&r, 280, 0, 328, 48);
- color_icon = TrapAvailable(_GetCIcon);
- if(color_icon) {
- icon_hdl = (Handle)GetCIcon(RSRC_NUMBER_BASE + WINDOW_ICON);
- PlotCIcon(&r, (CIconHandle)icon_hdl);
- }
- else {
- icon_hdl = GetIcon(RSRC_NUMBER_BASE + WINDOW_ICON);
- PlotIcon(&r, icon_hdl);
- }
- SetRect(&r, 1, 0, 49, 48);
- PenPat(gray_pat);
- PenMode(patXor);
- FrameRect(&r);
- while(!exit_draw) {
- FrameRect(&r);
- OffsetRect(&r, 2, 0);
- FrameRect(&r);
- if(r.left == (280 - 47)) {
- exit_draw = true;
- if(color_icon)
- PlotCIcon(&r, (CIconHandle)icon_hdl);
- else
- PlotIcon(&r, icon_hdl);
- }
- }
- while(!(WaitNextEvent(mDownMask, &an_event, 100, 0)));
- GetKeys(my_keymap);
- if( (BitTst(my_keymap, COMMAND_KEY)) && (BitTst(my_keymap, OPTION_KEY))) {
- EraseRect(&r);
- OffsetRect(&r, 47, 0);
- if(color_icon)
- PlotCIcon(&r, (CIconHandle)icon_hdl);
- else
- PlotIcon(&r, icon_hdl);
- OffsetRect(&r, 0, 48);
- if(color_icon)
- PlotCIcon(&r, (CIconHandle)icon_hdl);
- else
- PlotIcon(&r, icon_hdl);
- SysBeep(1);
- while(!(WaitNextEvent(mDownMask, &an_event, 100, 0)));
- }
- if(color_icon)
- DisposeCIcon((CIconHandle)icon_hdl);
- else
- DisposeHandle(icon_hdl);
- DisposeWindow(wp);
- SetPort(saved_port);
- SetPenState(&saved_pen_state);
- if(color_qd) {
- RGBForeColor(&fore_color);
- RGBBackColor(&back_color);
- }
- FlushEvents(everyEvent, 0);
- }
-